package system

import (
	"project-info/src/handle/response"
	"project-info/src/model"

	"project-info/src/lib")

// DepartmentsRes 部门列表响应参数
type DepartmentsRes struct {
	DepartmentKey   string `json:"departmentKey"`
	DepartmentName  string `json:"departmentName"`
	Id              string `json:"id"`
	ParentId        string `json:"parentId"`
	DepartmentNames string `json:"departmentNames"`
}

// 结构生成一个Employee结构体
type EmployeesRes struct {
	Id             uint64 `json:"id"`
	DepartmentId   uint64 `json:"departmentId"`
	DepartmentName string `json:"departmentName"`
	EmployeeName   string `json:"employeeName"`
	IsLeave        int    `json:"isLeave"`
	Open           bool   `json:"open"`
}

// GetDepartmentsReq 获取部门列表请求参数
type GetDepartmentsReq struct {
	ParentId uint64 `json:"parentId"`
}

// GetDepartmentsRes 获取部门列表响应参数
type GetDepartmentsRes struct {
	ChildDepartments []DepartmentsRes `json:"childDepartments"`
	Employees        []EmployeesRes   `json:"employees"`
	TitleDepartments []any            `json:"titleDepartments"`
}

// GetDepartmentsList 获取部门列表
func GetDepartmentsList(c *lib.GinContext) error {
	req := &GetDepartmentsReq{}
	err := c.Bind(req)
	if err != nil {
		c.Logger().Error(err.Error())
		return response.JsonFail(c, "参数绑定失败")
	}

	var users []model.User
	err = model.DB().Model(&model.User{}).Find(&users).Error
	if err != nil {
		c.Logger().Error(err.Error())
		return response.JsonFail(c, "查询失败")
	}

	departments := []DepartmentsRes{}
	employees := []EmployeesRes{}
	for _, user := range users {
		employees = append(employees, EmployeesRes{
			Id:           user.Id,
			EmployeeName: user.Username,
		})
	}

	res := &GetDepartmentsRes{
		ChildDepartments: departments,
		Employees:        employees,
		TitleDepartments: []any{},
	}

	return response.JsonOk(c, res, "查询成功")
}
